import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.*;

/**
 * 
 * @author Srinidhi Chaganty
 *
 */
public class LandOfficePanel extends TransactionPanel{
	
	private LandOffice lo;
	private Player p;
	private Map m;
	private JPanel totalRep;
	private JPanel landRep;
	private JPanel buyRep;
	private JPanel sellRep;
	
	public LandOfficePanel(LandOffice land){
		lo = land;
		p =  GameModel.getInstance().getCurrPlayer();
		m = GameModel.getInstance().getMap();
	}
	
	@Override
	JLabel introLabel() {
		return new JLabel("Welcome to the land office what do you want to do?");
	}
	
	@Override
	public JPanel generateBuy(){
		try {
			buyRep = m.generatePanel();
			PicButton[] arr = m.getButtonArr();
			for(int i = 0; i < 45; i++){
				final PicButton j = arr[i];
				final Property prop = m.getProperty(i);
				if(prop.getTerrainNum() != 6){
					j.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							JFrame frame = new JFrame();
							if(prop.getOwner() != null){
								JOptionPane.showMessageDialog(frame,"Property is already owned");
							}
							else{
								if(lo.sellProp(p, prop)){
									JOptionPane.showMessageDialog(frame,"Bought property!");
								}
								else{
									JOptionPane.showMessageDialog(frame,"You cannot afford that!");
								}
							}
						}
						
					}); 
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buyRep;
	}
	
	@Override
	public JPanel generateSell(){
		try {
			sellRep = m.generatePanel();
			JButton[] arr = m.getButtonArr();
			for(int i = 0; i < 45; i++){
				JButton j = arr[i];
				final Property prop = m.getProperty(i);
				j.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent e) {
						JFrame frame = new JFrame();
						if(prop.getOwner() != null && prop.getOwner().getName().equals(p.getName())){
							if(lo.buyProp(p, prop)){
								JOptionPane.showMessageDialog(frame,"You sold that property!");
							}
						}
						//if the property is owned by another person or not at all
						else{
							JOptionPane.showMessageDialog(frame,"You do not own that property.");
						}
					}
					
				});
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sellRep;
	}

	
}
